//
//  JXSelectViewHandle.m
//  UILbrary
//
//  Created by Apple on 2023/2/21.
//  Copyright © 2023 mac. All rights reserved.
//

#import "JXSelectViewHandle.h"
#import "NSArray+Sudoku.h"
#import "MYHeader.h"


@implementation JXSecondHandDropViewSelectModel

- (instancetype)init
{
    self = [super init];
    if(self){
        _idx = -1;
        _isSelect = NO;
    }
    return self;
}
@end


@interface JXSelectItemView : UIView
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) JXSecondHandDropViewSelectModel *model;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, copy) void(^selectBlock)(void);

@end

@implementation JXSelectItemView

//MARK: - system
- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self p_configSubviews];
    }
    return self;
}

//MARK: - publick

- (void)configWithModel:(JXSecondHandDropViewSelectModel *)model
{
    self.model = model;
    self.titleLabel.text = model.title;
    model.isSelect ? [self p_selectStatu] : [self p_noSelectStatu];
}

- (void)selectState:(BOOL)selected
{
    self.model.isSelect = selected;
    selected ? [self p_selectStatu] : [self p_noSelectStatu];
}

//MARK: - private
- (void)p_configSubviews
{
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.titleLabel];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0 ));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0 ));
    }];
    
}

- (void)p_selectStatu
{
    self.titleLabel.backgroundColor = [UIColor JX_RedColor_4];
    self.titleLabel.textColor = [UIColor JX_RedColor];
}

- (void)p_noSelectStatu
{
    self.titleLabel.backgroundColor = [UIColor JX_GrayColor_8];
    self.titleLabel.textColor = [UIColor blackColor];
}

//MARK: - action

- (void)tapAction
{
    if(self.selectBlock)self.selectBlock();
}


//MARK: - get

- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [UIView new];
        [_contentView addGestureRecognizer:self.tap];
    }
    return _contentView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel *label = [UILabel new];
        label.textColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:11.0];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UITapGestureRecognizer *)tap
{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    }
    return _tap;
}

//MARK: - test

- (void)test
{
    [self test_1];
}

- (void)test_1
{
    
}


@end

@interface JXSelectIView : UIView

@property(nonatomic, strong) JXSecondHandDropViewSelectModel *selectModel;
@property (nonatomic, copy) void (^selectBlock)(JXSecondHandDropViewSelectModel *selectModel);

@end

@implementation JXSelectIView

- (void)configWithDatas:(NSArray <JXSecondHandDropViewSelectModel *>*)datas
{
    [datas enumerateObjectsUsingBlock:^(JXSecondHandDropViewSelectModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        JXSelectItemView *view = [JXSelectItemView new];
        [view configWithModel:obj];
        view.layer.cornerRadius = 5.0;
        view.clipsToBounds = YES;
        @WeakObj(self);
        @WeakObj(view);
        view.selectBlock = ^{
            @StrongObj(self);
            @StrongObj(view);
            [self p_cleanAllItemViewSelectStatus];
            [view selectState:YES];
            self.selectModel = view.model;
            if(self.selectBlock) self.selectBlock(self.selectModel);
        };
        [self addSubview:view];
    }];
    
    NSInteger warpCount = 3;
    CGFloat ItemWidth = 0;
    CGFloat marge = 15;
//    CGFloat fixedInteritemSpacing = (SCREEN_WIDTH - warpCount * ItemWidth - marge * 2) / (warpCount - 1);
    CGFloat fixedInteritemSpacing = 13;

    CGFloat fixedItemHeight = 28;
    CGFloat fixedLineSpacing = 10;
    [self.subviews mas_distributeSudokuViewsWithFixedItemWidth:ItemWidth fixedItemHeight:fixedItemHeight fixedLineSpacing:fixedLineSpacing fixedInteritemSpacing:fixedInteritemSpacing warpCount:warpCount topSpacing:marge bottomSpacing:marge leadSpacing:marge tailSpacing:marge];
}

- (void)p_cleanAllItemViewSelectStatus
{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof JXSelectItemView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj selectState:NO];
    }];
}

@end


@interface JXSelectViewHandle()

@property (nonatomic, strong) JXSelectIView *contentView;

@end


@implementation JXSelectViewHandle

- (instancetype)init
{
    self = [super init];
    if(self){
    }
    return self;
}

- (void)configWithDatas:(NSArray <JXSecondHandDropViewSelectModel *>*)datas
{
    [self.contentView configWithDatas:datas];
}

- (UIView *)view
{
    return self.contentView;
}

- (JXSelectIView *)contentView
{
    if(!_contentView){
        _contentView = [JXSelectIView new];
        _contentView.selectBlock = self.selectBlock;
    }
    return _contentView;
}
@end
